//
//  HDCFWisdomTestResultViewController.m
//  HaoDeCaiFu
//  Created by 张玉鹏 on 2017/11/15.
//  Copyright © 2017年 郑开宏. All rights reserved.
//
#import "HDCFWisdomTestResultViewController.h"
#import "HDCFMakeUpTableViewController.h"
#import "UIColor+hex.h"
#import "HDCFWisdomRiskTestVc.h"


@interface HDCFWisdomTestResultViewController ()
@property (nonatomic, weak) UILabel *descriptionLabel;
@property (nonatomic, weak) UILabel *titlL;
@end

@implementation HDCFWisdomTestResultViewController

- (void)setRiskType:(NSString *)riskType{
    _riskType = riskType;
}

- (void)setPortfolioCode:(NSString *)portfolioCode{
    _portfolioCode = portfolioCode;
}


- (void)getPersonInfo{
    [SVProgressHUD show];
    NSDictionary *dict = @{@"assess":_riskType};
    [HttpRequest sendTwoSelfGetRequestWithURL:@"api/SiteConfig/GetRecommend" WithParamets:dict success:^(id result) {
        if ([result[@"code"] isEqual:@1]) {
            [SVProgressHUD dismiss];
            NSDictionary *dict = result[@"result"];
            CGSize size = [self sizeWithText:dict[@"Remark"] font:[UIFont systemFontOfSize:16]];
            self.descriptionLabel.frame = CGRectMake(10, CGRectGetMaxY(_titlL.frame) + 15, screenWidth - 20, size.height);
            self.descriptionLabel.text = dict[@"Remark"];
            
            self.portfolioCode = dict[@"portfolioCode"];
        }else{
            [SVProgressHUD showInfoWithStatus:result[@"msg"]];
            [SVProgressHUD dismissWithDelay:1.0];
        }
    } failure:^(NSError *errer) {
        [SVProgressHUD showInfoWithStatus:@"服务器错误"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];
}

-(void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBarHidden=NO;
}

-(void)viewWillAppear:(BOOL)animated
{
     self.navigationController.navigationBarHidden=YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    UIImageView*bgImageView=[[UIImageView alloc]initWithFrame:CGRectMake((screenWidth - 200) / 2, 64, 200, 200)];
    bgImageView.image=[UIImage imageNamed:self.style];
    [self.view addSubview:bgImageView];
    
    UILabel*titleLable=[[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(bgImageView.frame) + 15, screenWidth, 20)];
    self.titlL = titleLable;
    NSString *strL = [NSString stringWithFormat:@"您的投资类型为：%@", [self returnType:self.style]];
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc] initWithString:strL];
    [AttributedStr addAttribute:NSFontAttributeName
                          value:[UIFont systemFontOfSize:15]
                          range:NSMakeRange(0, strL.length)];
    [AttributedStr addAttribute:NSForegroundColorAttributeName
                          value:[self returnColor:self.style]
                          range:NSMakeRange(8, strL.length - 8)];
    [AttributedStr addAttribute:NSForegroundColorAttributeName
                          value:[UIColor whiteColor]
                          range:NSMakeRange(0, 8)];

    titleLable.font=[UIFont systemFontOfSize:17];
    titleLable.textAlignment=1;
    titleLable.attributedText = AttributedStr;
    [self.view addSubview:titleLable];
    
    UILabel *riskDescriptionLabel = [[UILabel alloc] init];
    self.descriptionLabel = riskDescriptionLabel;
    CGSize size = [self sizeWithText:@"风险描述：" font:[UIFont systemFontOfSize:16]];
    riskDescriptionLabel.frame = CGRectMake(10, CGRectGetMaxY(titleLable.frame) + 15, screenWidth - 20, size.height);
    riskDescriptionLabel.textColor = [UIColor whiteColor];
    riskDescriptionLabel.numberOfLines = 0;
    riskDescriptionLabel.text = @"风险描述：";
    riskDescriptionLabel.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:riskDescriptionLabel];
    
    UIButton*backBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame=CGRectMake(10, W_RATIO(50), W_RATIO(60), W_RATIO(60));
    [backBtn setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn];
    
    UILabel *labelti = [[UILabel alloc] init];
    labelti.frame = CGRectMake((screenWidth - 100) / 2, W_RATIO(50), 100, W_RATIO(60));
    [self.view addSubview:labelti];
    labelti.textColor = [UIColor whiteColor];
    labelti.text = @"智能投顾";
    labelti.font = [UIFont boldSystemFontOfSize:18];
    
    UIButton*makeUpBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    makeUpBtn.frame=CGRectMake(W_RATIO(110), screenHeight-W_RATIO(300), screenWidth-W_RATIO(220), W_RATIO(100));
    [makeUpBtn setTitle:@"查看您的专享基金组合" forState:UIControlStateNormal];
    NSString*imageName=[NSString stringWithFormat:@"button_%@",self.style];
    [makeUpBtn setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [makeUpBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [makeUpBtn addTarget:self action:@selector(makeUpBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    makeUpBtn.titleLabel.font=[UIFont systemFontOfSize:15];
    [self.view addSubview:makeUpBtn];
    
    UIButton*newTestBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    newTestBtn.frame=CGRectMake(screenWidth-120, W_RATIO(50), 110, W_RATIO(60));
    [newTestBtn setTitle:@"重新评测" forState:UIControlStateNormal];
   newTestBtn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentRight;
    [newTestBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [newTestBtn addTarget:self action:@selector(newTestBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    newTestBtn.titleLabel.font=[UIFont systemFontOfSize:15];
    [self.view addSubview:newTestBtn];
    [self getPersonInfo];
}

- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxW:(CGFloat)maxW
{
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = font;
    CGSize maxSize = CGSizeMake(maxW, MAXFLOAT);
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}

- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font
{
    return [self sizeWithText:text font:font maxW:(self.view.width - 60)];
}

-(void)setStyle:(NSString *)style
{
    self.riskType = style;
    if ([style isEqualToString:@"夏普"]||[style isEqualToString:@"夏普型"]) {
        _style=@"xiapu";
    }
    if ([style isEqualToString:@"进取型"]||[style isEqualToString:@"进取"]) {
        _style=@"jinqu";
    }
//    style    __NSCFString *    @"保守型"    0x000060400063e160
    if ([style isEqualToString:@"中度保守型"]||[style isEqualToString:@"保守型"]) {
        _style=@"baoshou";
    }
    if ([style isEqualToString:@"环球"]||[style isEqualToString:@"环球型"]) {
        _style=@"huanqiu";
    }
    if ([style isEqualToString:@"平衡"]||[style isEqualToString:@"平衡型"]) {
        _style=@"pingheng";
    }
    if ([style isEqualToString:@"中度进取型"]||[style isEqualToString:@"中度进取"]) {
        _style=@"zhongdujinqu";
    }

}

-(NSString *)returnType:(NSString *)type
{
    if ([type isEqualToString:@"xiapu"]||[type isEqualToString:@"夏普型"]) {
        self.view.backgroundColor = [UIColor colorWithHex:0x9E59B0];
        return @"夏普型";
    }
    if ([type isEqualToString:@"jinqu"]||[type isEqualToString:@"进取"]) {
        self.view.backgroundColor = [UIColor colorWithHex:0xF74E33];
        return @"进取型";
    }
    if ([type isEqualToString:@"baoshou"]||[type isEqualToString:@"中度保守"]) {
        self.view.backgroundColor = [UIColor colorWithHex:0x21A0E0];
         return @"中度保守型";
    }
    if ([type isEqualToString:@"huanqiu"]||[type isEqualToString:@"环球型"]) {
        self.view.backgroundColor = [UIColor colorWithHex:0x6D5AB5];
        return @"环球型";
    }
    if ([type isEqualToString:@"pingheng"]||[type isEqualToString:@"平衡型"]) {
        self.view.backgroundColor = [UIColor colorWithHex:0x4A69C3];
        return @"平衡型";
    }
    if ([type isEqualToString:@"zhongdujinqu"]||[type isEqualToString:@"中度进取"]) {
        self.view.backgroundColor = [UIColor colorWithHex:0xD96D7D];
        return @"中度进取型";
    }
    return nil;
}

- (UIColor *)returnColor:(NSString *)type
{
    if ([type isEqualToString:@"xiapu"]||[type isEqualToString:@"夏普型"]) {
        return [UIColor colorWithHex:0x490565];
    }
    if ([type isEqualToString:@"jinqu"]||[type isEqualToString:@"进取"]) {
        return [UIColor colorWithHex:0x760c05];
    }
    if ([type isEqualToString:@"baoshou"]||[type isEqualToString:@"中度保守"]) {
        return [UIColor colorWithHex:0x00356a];
    }
    if ([type isEqualToString:@"huanqiu"]||[type isEqualToString:@"环球型"]) {
        return [UIColor colorWithHex:0x352578];
    }
    if ([type isEqualToString:@"pingheng"]||[type isEqualToString:@"平衡型"]) {
        return [UIColor colorWithHex:0x152055];
    }
    if ([type isEqualToString:@"zhongdujinqu"]||[type isEqualToString:@"中度进取"]) {
        return [UIColor colorWithHex:0x923742];
    }
    return nil;
    
}

-(void)makeUpBtnClick:(UIButton*)btn
{
    HDCFMakeUpTableViewController *tVc = [[HDCFMakeUpTableViewController alloc] init];
    tVc.portfolioCode=self.portfolioCode;
    [self.navigationController pushViewController:tVc animated:YES];
    
}

- (void)setIsFromHome:(BOOL)isFromHome{
    _isFromHome = isFromHome;
}

-(void)newTestBtnClick:(UIButton*)btn
{
    if (_isFromHome == YES) {
        HDCFWisdomRiskTestVc *vc = [[HDCFWisdomRiskTestVc alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        self.NewTestButtonActionBlock();
        [self.navigationController popViewControllerAnimated:YES];
    }
}
-(void)backBtnClick:(UIButton*)btn
{
    self.navigationController.navigationBarHidden=NO;
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
